@echo off
REM Use a legacy code page to avoid Python 2.7 cp65001 issue
chcp 936 >nul

REM Change to script directory to avoid relative path issues
cd /d %~dp0

echo Web Tools Build Script

echo.Checking Python 2.7 environment...
REM Prefer Windows Python launcher (py -2); fallback to python and check 2.7
set "PY_CMD="

where py >nul 2>&1
if %errorlevel%==0 (
    py -2 -V >nul 2>&1
    if %errorlevel%==0 (
        set "PY_CMD=py -2"
    )
)

if not defined PY_CMD (
    python --version 2>__tmp_pyver.txt 1>nul
    if errorlevel 1 (
        echo Error: Python not installed or not in PATH
        del __tmp_pyver.txt 2>nul
        pause
        exit /b 1
    )
    findstr /r "^Python 2\.7\." __tmp_pyver.txt >nul
    if errorlevel 1 (
        echo Error: Python 2.7 required. Current: && type __tmp_pyver.txt
        del __tmp_pyver.txt 2>nul
        pause
        exit /b 1
    )
    del __tmp_pyver.txt 2>nul
    set "PY_CMD=python"
)

echo.Checking build script...
REM Ensure build_exe.py exists
if not exist build_exe.py (
    echo Error: build_exe.py not found.
    pause
    exit /b 1
)

echo.Running build...
REM Run build script
%PY_CMD% build_exe.py

REM Check build result
if errorlevel 1 (
    echo.Build failed.
) else (
    echo.Build completed successfully.
    if exist dist (
        echo.Files in dist folder:
        dir dist
    )
)

pause